﻿using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

public enum PlayerAndChar
{
    CHARA,
    CHARB,
    CHARACHARB,
    CHARBCHARA,
    NONE
}

class CharacterChooseScreen : MenuScreen
{
    SpriteFont font;

    private Texture2D backgroundTexture;
    private Vector2 backgroundPosition;

    MenuEntry charAEntry, charBEntry, charAcharBEntry, charBcharAEntry, goToPreviousEntry;

    private Texture2D buttonCHARA, buttonCHARB;
    private Texture2D buttonCHARACHARB, buttonCHARBCHARA;
    private Texture2D previous;

    public CharacterChooseScreen()
        : base()
    {
        Direction = MenuEntryDirection.CHARSELECTION;

        charAEntry = new MenuEntry("");
        charAEntry.Description = "Start a new Game";
        charAEntry.Position = new Vector2(-3, 0);
        charAEntry.Selected += CharBSelected;
        MenuEntries.Add(charAEntry);        

        charBEntry = new MenuEntry("");
        charBEntry.Description = "Start a new Game";
        charBEntry.Position = new Vector2(586, 0);
        charBEntry.Selected += CharASelected;
        MenuEntries.Add(charBEntry);

        charAcharBEntry = new MenuEntry("");
        charAcharBEntry.Description = "Start a new Game";
        charAcharBEntry.Position = new Vector2(-3, 152);
        charAcharBEntry.Selected += CharBCharASelected;
        MenuEntries.Add(charAcharBEntry);

        charBcharAEntry = new MenuEntry("");
        charBcharAEntry.Description = "Start a new Game";
        charBcharAEntry.Position = new Vector2(582, 152f);
        charBcharAEntry.Selected += CharACharBSelected;
        MenuEntries.Add(charBcharAEntry);

        goToPreviousEntry = new MenuEntry("");
        goToPreviousEntry.Description = "Return to Main Menu";
        goToPreviousEntry.Position = new Vector2(0, 500);
        goToPreviousEntry.Selected += OnCancel;
        MenuEntries.Add(goToPreviousEntry);
    }

    /// <summary>
    /// Load the graphics content for this screen.
    /// </summary>
    public override void LoadContent()
    {
        // load the textures
        ContentManager content = ScreenManager.Game.Content;

        font = content.Load<SpriteFont>("SpriteFont1");

        //set font on each entry menu
        charAEntry.Font = font;
        charAcharBEntry.Font = font;
        charBEntry.Font = font;
        charBcharAEntry.Font = font;
        goToPreviousEntry.Font = font;

        backgroundTexture = content.Load<Texture2D>(@"Textures\MenuMock\Menu_player_personagens 2 Cinza");

        buttonCHARA = content.Load<Texture2D>(@"Textures\MenuMock\SINGLE");
        buttonCHARB = content.Load<Texture2D>(@"Textures\MenuMock\single2");

        buttonCHARACHARB = content.Load<Texture2D>(@"Textures\MenuMock\coop");
        buttonCHARBCHARA = content.Load<Texture2D>(@"Textures\MenuMock\coop2");

        previous = content.Load<Texture2D>(@"Textures\MenuMock\BACK");

        // calculate the texture positions
        Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
        backgroundPosition = new Vector2(
            (viewport.Width - backgroundTexture.Width) / 2,
            (viewport.Height - backgroundTexture.Height) / 2);

        // set the textures on each menu entry
        charAEntry.SelectedTexture = buttonCHARA;
        charBEntry.SelectedTexture = buttonCHARB;
        charAcharBEntry.SelectedTexture = buttonCHARACHARB;
        charBcharAEntry.SelectedTexture = buttonCHARBCHARA;

        goToPreviousEntry.SelectedTexture = previous;

        base.LoadContent();
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            ExitScreen();
            return;
        }

        base.HandleInput();
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        // draw the background images
        spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);

        // Draw each menu entry in turn.
        for (int i = 0; i < MenuEntries.Count; i++)
        {
            MenuEntry menuEntry = MenuEntries[i];
            bool isSelected = IsActive && (i == selectedEntry);
            menuEntry.Draw(this, isSelected, gameTime);
        }

        spriteBatch.End();
    }

    void CharASelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new GameplayScreen(PlayerAndChar.CHARA));
    }

    void CharBSelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new GameplayScreen(PlayerAndChar.CHARB));
    }

    void CharACharBSelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new GameplayScreen(PlayerAndChar.CHARACHARB));
    }

    void CharBCharASelected(object sender, EventArgs e)
    {
        ScreenManager.AddScreen(new GameplayScreen(PlayerAndChar.CHARBCHARA));
    }
}
